Skip to content

Add proxy support option#50

Merged
obeone merged 4 commits into
mainfrom
codex/ajouter-option-pour-utiliser-un-proxy
Jul 9, 2025
Merged

Add proxy support option#50
obeone merged 4 commits into
mainfrom
codex/ajouter-option-pour-utiliser-un-proxy

Conversation

@obeone

@obeone obeone commented Jul 9, 2025

Copy link
Copy Markdown
Owner

Summary

  • allow using a proxy with --proxy CLI flag
  • pass proxy information to Scraper
  • support proxy in Scraper via requests.Session
  • document proxy option in README
  • test proxy initialization and CLI option

Testing

  • pytest -q

https://chatgpt.com/codex/tasks/task_e_686e78addf04832e9325f74ff9395904

Summary by CodeRabbit

  • New Features
    • Added support for specifying a proxy URL for HTTP and SOCKS requests via a new --proxy command-line option.
  • Documentation
    • Updated usage instructions and options in the README to include the new proxy feature.
  • Tests
    • Added tests to verify correct handling and initialization of the proxy option in both the CLI and scraper components.
    • Added tests to confirm behavior when proxy connectivity fails and when scraping yields empty content.

@coderabbitai

coderabbitai Bot commented Jul 9, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

The changes introduce an optional --proxy argument to the command-line interface, allowing users to specify a proxy URL for HTTP or SOCKS requests made by the scraper. The Scraper class is updated to support proxy configuration via a session object, and relevant documentation and tests are added or updated to reflect and verify this new functionality.

Changes

File(s) Change Summary
README.md Documented the new --proxy <PROXY_URL> CLI argument; updated usage example and options list.
crawler_to_md/cli.py Added --proxy CLI argument; passed proxy to Scraper constructor with error handling.
crawler_to_md/scraper.py Updated Scraper to accept proxy, configure session proxies, test proxy connectivity, and use session for requests.
tests/test_cli.py Added tests verifying CLI --proxy option passes correct proxy URL to Scraper and error handling.
tests/test_scraper.py Changed HTTP GET monkeypatch to session; added tests for proxy initialization and failure detection in Scraper.
pyproject.toml Updated requests dependency to requests[socks] to enable SOCKS proxy support.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI
    participant Scraper
    participant HTTP_Server

    User->>CLI: Run with --proxy <PROXY_URL>
    CLI->>Scraper: Instantiate Scraper(proxy=<PROXY_URL>)
    Scraper->>Scraper: Configure requests.Session with proxy
    Scraper->>HTTP_Server: Make HTTP requests via proxy
Loading

Poem

A proxy hops into the code,
Through tunnels where requests are flowed.
Now scrapers can sneak,
With a click or a tweak,
And test bunnies cheer on the road!
🐇🌐🕳️


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d0bc49e and e0437f1.

📒 Files selected for processing (2)
  • crawler_to_md/scraper.py (5 hunks)
  • tests/test_scraper.py (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • crawler_to_md/scraper.py
  • tests/test_scraper.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build-and-push
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
crawler_to_md/scraper.py (1)

47-50: LGTM! Proxy configuration implemented correctly.

The implementation properly:

  • Uses requests.Session for better connection management
  • Configures proxy for both HTTP and HTTPS protocols
  • Stores the proxy reference for potential future use

Consider adding basic URL validation for the proxy parameter to catch common formatting errors early.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0529c6a and af7778a.

📒 Files selected for processing (5)
  • README.md (2 hunks)
  • crawler_to_md/cli.py (2 hunks)
  • crawler_to_md/scraper.py (4 hunks)
  • tests/test_cli.py (1 hunks)
  • tests/test_scraper.py (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/test_scraper.py (1)
crawler_to_md/scraper.py (1)
  • Scraper (19-291)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build-and-push
🔇 Additional comments (11)
README.md (2)

55-55: LGTM! Documentation updated correctly.

The usage example properly includes the new --proxy option in the command line example.


70-70: LGTM! Clear option description.

The proxy option documentation is concise and appropriately describes its purpose.

crawler_to_md/scraper.py (4)

27-27: LGTM! Proxy parameter added correctly.

The proxy parameter is properly added with appropriate default value.


39-39: LGTM! Docstring updated appropriately.

The parameter documentation is clear and follows the existing format.


88-88: LGTM! Updated to use session.

Correctly replaced requests.get with self.session.get to utilize the configured proxy.


242-242: LGTM! Consistent session usage.

Correctly replaced requests.get with self.session.get for consistent proxy usage throughout the scraper.

tests/test_scraper.py (2)

155-155: LGTM! Mock target updated correctly.

The monkeypatch correctly targets scraper.session.get instead of requests.get to match the implementation change.


176-182: LGTM! Comprehensive proxy test added.

The test effectively verifies that:

  • Proxy parameter is correctly passed to the Scraper constructor
  • Session proxies are configured for both HTTP and HTTPS protocols
  • The proxy URL is properly stored in the session

The test follows existing patterns and provides good coverage for the new functionality.

tests/test_cli.py (1)

48-82: LGTM! Thorough CLI proxy test.

The test effectively verifies that:

  • The --proxy CLI argument is correctly parsed
  • The proxy value is properly passed to the Scraper constructor
  • The test uses appropriate mocking to isolate the functionality being tested

The test follows existing patterns and provides good coverage for the new CLI option.

crawler_to_md/cli.py (2)

91-95: LGTM! Proxy argument added correctly.

The --proxy argument is properly configured with:

  • Appropriate help text describing its purpose
  • Default value of None when not specified
  • Consistent with other CLI argument patterns

180-180: LGTM! Proxy argument passed correctly.

The proxy argument is correctly passed to the Scraper constructor, completing the integration of the proxy feature through the CLI.

coderabbitai Bot added a commit that referenced this pull request Jul 9, 2025
Docstrings generation was requested by @obeone.

* #50 (comment)

The following files were modified:

* `crawler_to_md/cli.py`
* `crawler_to_md/scraper.py`
* `tests/test_cli.py`
* `tests/test_scraper.py`
@coderabbitai

coderabbitai Bot commented Jul 9, 2025

Copy link
Copy Markdown
Contributor

Note

Generated docstrings for this pull request at #51

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (4)
tests/test_cli.py (4)

51-85: Refactor to reduce code duplication.

The fake_init function and monkeypatching setup are duplicated across multiple test functions. Consider extracting common setup logic into a helper function or fixture.

+def _setup_proxy_test(monkeypatch, captured):
+    def fake_init(
+        self,
+        base_url,
+        exclude_patterns,
+        db_manager,
+        rate_limit=0,
+        delay=0,
+        proxy=None,
+    ):
+        captured['proxy'] = proxy
+
+    monkeypatch.setattr(Scraper, '__init__', fake_init)
+    monkeypatch.setattr(Scraper, 'start_scraping', lambda *a, **k: None)
+    monkeypatch.setattr(ExportManager, 'export_to_markdown', lambda *a, **k: None)
+    monkeypatch.setattr(ExportManager, 'export_to_json', lambda *a, **k: None)
+
 def test_cli_proxy_option(monkeypatch, tmp_path):
     captured = {}
-
-    def fake_init(
-        self,
-        base_url,
-        exclude_patterns,
-        db_manager,
-        rate_limit=0,
-        delay=0,
-        proxy=None,
-    ):
-        captured['proxy'] = proxy
-
-    monkeypatch.setattr(Scraper, '__init__', fake_init)
-    monkeypatch.setattr(Scraper, 'start_scraping', lambda *a, **k: None)
-    monkeypatch.setattr(ExportManager, 'export_to_markdown', lambda *a, **k: None)
-    monkeypatch.setattr(ExportManager, 'export_to_json', lambda *a, **k: None)
+    _setup_proxy_test(monkeypatch, captured)

87-121: Use consistent quote style.

The test uses single quotes while the rest of the file uses double quotes. Maintain consistency with the existing codebase.

-    cache_folder = tmp_path / 'cache'
+    cache_folder = tmp_path / "cache"
-        'prog',
-        '--url',
-        'http://example.com',
-        '--output-folder',
+        "prog",
+        "--url",
+        "http://example.com",
+        "--output-folder",

159-179: Consider adding more specific error validation.

The test only verifies that SystemExit is raised but doesn't validate the error message or exit code. Consider adding assertions for more specific error handling.

+import contextlib
+import io
+
 def test_cli_proxy_error(monkeypatch, tmp_path):
     def fake_init(*a, **k):
         raise ValueError('Proxy unreachable')
 
     monkeypatch.setattr(Scraper, '__init__', fake_init)
+    
+    # Capture stderr to verify error message
+    stderr_capture = io.StringIO()
+    
     cache_folder = tmp_path / 'cache'
     args = [
         'prog',
         '--url',
         'http://example.com',
         '--output-folder',
         str(tmp_path),
         '--cache-folder',
         str(cache_folder),
         '--proxy',
         'http://proxy:8080',
     ]
     monkeypatch.setattr(sys, 'argv', args)
-    with pytest.raises(SystemExit):
-        cli.main()
+    
+    with contextlib.redirect_stderr(stderr_capture):
+        with pytest.raises(SystemExit) as exc_info:
+            cli.main()
+    
+    assert exc_info.value.code == 1
+    assert 'Proxy unreachable' in stderr_capture.getvalue()

51-179: Consider adding edge case tests.

The current test suite covers the main scenarios but could benefit from additional edge cases to ensure robustness.

Additional test cases to consider:

  • Empty proxy string
  • Malformed proxy URLs
  • Proxy with authentication
  • IPv6 proxy addresses
def test_cli_proxy_empty_string(monkeypatch, tmp_path):
    # Test behavior with empty proxy string
    pass

def test_cli_proxy_malformed_url(monkeypatch, tmp_path):
    # Test behavior with malformed proxy URL
    pass
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9483b05 and d0bc49e.

📒 Files selected for processing (5)
  • README.md (2 hunks)
  • crawler_to_md/cli.py (2 hunks)
  • crawler_to_md/scraper.py (4 hunks)
  • tests/test_cli.py (2 hunks)
  • tests/test_scraper.py (3 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • README.md
  • crawler_to_md/cli.py
  • tests/test_scraper.py
  • crawler_to_md/scraper.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build-and-push
🔇 Additional comments (2)
tests/test_cli.py (2)

2-4: LGTM: Import changes are appropriate.

The addition of pytest import and formatting adjustment are correct for the new test functionality.


123-157: Good coverage of SOCKS proxy support.

This test appropriately verifies that SOCKS proxy URLs are handled correctly, which is important given the requests[socks] dependency mentioned in the summary.

@obeone obeone merged commit 53d1a4f into main Jul 9, 2025
6 checks passed
@obeone obeone deleted the codex/ajouter-option-pour-utiliser-un-proxy branch July 13, 2025 16:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant